//流程管理
layui.define(['jquery', 'func', "laytpl", 'layer', 'action', 'conf', 'binder', 'form', 'element', 'api', 'part', 'authority'], function (exports) {
    const { action, jquery: $, binder, conf, laytpl, api, layer, func, part, authority } = layui;


    exports("process", {
        route: function () {
            var router = layui.router();

            //重写当前路由(只执行一次)
            let route = router.href || '/';

            //检查登录
            if (!localStorage.getItem('user_token')) {
                route = conf.view.login;
            }

            if (route === '/') {
                //渲染布局
                route = conf.view.layout;
            }

            if ($.inArray(route, [conf.view.layout, conf.view.login]) < 0 && self == top) {
                // 缓存地址
                localStorage.setItem("auto_open_page", route);

                route = conf.view.layout;
            }

            return route;
        },
        //页面配置流程
        launch: function (route) {
            layui.link("./css/theme/" + conf.view.theme.curr + ".css");

            //layer只使用顶级窗口
            layer.config({ offset: '5%' });

            /** 全局配置 **/
            layui.table.config.text = {
                none: '<i class="layui-icon layui-icon-tabs"></i><br>无数据'
            };
            // layui.table.config.url = route;
            layui.table.config.page = true;
            layui.table.config.method = "post";
            layui.table.config.limit = 10;
            layui.table.config.skin = 'line';
            layui.table.config.elem = "table[lay-filter]";
            //默认状态值
            layui.table.config.response = { statusCode: 200 };
            layui.table.config.defaultToolbar = [];

            var originLayerTableRender = layui.table.render;
            layui.table.render = function (options) {
                if (conf.api.mock.use && options.url) {
                    options.method = "get";
                    const urlArray = options.url.split("?");
                    options.url = "/mock/api" + urlArray[0] + ".json?" + (urlArray[1]||"");

                }

                return originLayerTableRender(options);
            }


            //改写
            var _tableRender = layui.table.render;
            layui.table.render = function (options) {
                if (options.done) {
                    var _done = options.done;
                    options.done = function () {
                        //执行权限
                        authority.run();

                        //执行lay-part渲染
                        part.render();

                        _done();
                    }
                }
                _tableRender(options);
            }




            func.changeFavicon(conf.view.favicon);

        },
        //页面流程
        page: async function (data) {
            if (typeof data != "object") {
                throw new Error("必须为");
            }

            //继承基类
            data = $.extend(true, {}, action, data);
            const Target = {};


            Target.binder = data.binder;

            Target.beforeRender = data.beforeRender;
            Target.afterRender = data.afterRender;

            Target.resize = data.resize;
            Target.config = data.config;

            Target.param = func.param();

            Target.query = func.query();

            Object.keys(data.data).forEach(function (key) {
                Target[key] = data.data[key];
            })

            Object.keys(data.methods).forEach(function (name) {
                Target[name] = data.methods[name];
            })

            // Target.assign = data.assign;
            // if (Target.config.assign) {
            //     Target.assign = await api.assign(Target.config.assign, Target.param);
            // }

            await Target.beforeRender();

            Target.conf = conf;

            laytpl($('template').html()).render($.extend(true, {}, Target, {
                func: func
            }), async function (html) {

                $("body").append("<div id='content' class='layui-anim-upbit' style='visibility: hidden'>" + html + "</div>");

                $('template').remove();

                authority.run();

                //操作所有dom
                $('input').attr("autocomplete", "off");
                // $("option[selected='']").removeAttr("selected");

                //后置执行器
                await Target.afterRender();


                //事件绑定
                binder.run(Target, data.binder);

                //动态渲染part模块
                part.render();

                layui.element.render();
                layui.form.render();

                //显示页面
                $("body > #content").css("visibility", "visible");

                func.postMessage('render_success', {});
            });
        }
    });
});